package com.zxl.gtion.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxl.gtion.dao.CatalogDao;
import com.zxl.gtion.dto.ArticleDTO;
import com.zxl.gtion.entity.ArticleContent;
import com.zxl.gtion.entity.ArticleInfo;
import com.zxl.gtion.entity.Catalog;
import com.zxl.gtion.entity.CatalogArticle;
import com.zxl.gtion.enums.CatalogEnums;
import com.zxl.gtion.service.ArticleContentService;
import com.zxl.gtion.service.ArticleInfoService;
import com.zxl.gtion.service.CatalogArticleService;
import com.zxl.gtion.service.CatalogService;
import com.zxl.gtion.vo.VaccineVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (Catalog)表服务实现类
 *
 * @author
 * @since 2024-03-06 16:43:15
 */
@Service("catalogService")
@Slf4j
@RequiredArgsConstructor
public class CatalogServiceImpl extends ServiceImpl<CatalogDao, Catalog> implements CatalogService {

    private final ArticleInfoService articleInfoService;
    private final CatalogArticleService catalogArticleService;
    private final ArticleContentService contentService;

    @Override
    public List<ArticleInfo> homePage(Catalog catalog) {
        String type = catalog.getType();
        // 通过type找到新闻类型的类目
        List<Catalog> catalogs = this.lambdaQuery().eq(Catalog::getType, type).list();
        if (catalogs.isEmpty()) {
            // 判空
            return new ArrayList<>();
        }
        // 只需要类目id 用stream流实现
        List<Integer> catalogIds = catalogs.stream().map(Catalog::getId).collect(Collectors.toList());
        // 根据类目id找到关联的文章信息
        List<CatalogArticle> catalogArticles = catalogArticleService.lambdaQuery().in(CatalogArticle::getCatalogId, catalogIds).list();
        if (catalogArticles.isEmpty()) {
            // 判空
            // 如果该类目下没有文章就返回空
            return new ArrayList<>();
        }
        List<Integer> articleIds = catalogArticles.stream().map(CatalogArticle::getArticleId).collect(Collectors.toList());

        List<ArticleInfo> list = articleInfoService.lambdaQuery().in(ArticleInfo::getId, articleIds).list();
        for (ArticleInfo articleInfo : list) {
            Integer id = articleInfo.getId();
            ArticleContent byId = contentService.lambdaQuery().eq(ArticleContent::getArticleId, id).one();
            if (byId != null) {
                articleInfo.setContent(byId.getContent());
            }
            articleInfo.setTime(articleInfo.getCreateTime().toLocalDate());
        }
        return list;
    }

    @Override
    public List<Catalog> homeVaccine() {
        List<Catalog> catalogs = this.lambdaQuery().in(Catalog::getType, CatalogEnums.getVaccines()).list();
        if (catalogs.isEmpty()) {
            return new ArrayList<>();
        }
        return catalogs;
    }

    @Override
    public Page getArticlePage(Page<ArticleInfo> page, ArticleDTO articleDTO) {
        String type = articleDTO.getType();
        List<Catalog> catalogs = this.lambdaQuery().eq(Catalog::getType, type).list();
        if (catalogs.isEmpty()) {
            // 判空
            return page;
        }
        // 只需要类目id 用stream流实现
        List<Integer> catalogIds = catalogs.stream().map(Catalog::getId).collect(Collectors.toList());
        // 根据类目id找到关联的文章信息
        List<CatalogArticle> catalogArticles = catalogArticleService.lambdaQuery().in(CatalogArticle::getCatalogId, catalogIds).list();
        if (catalogArticles.isEmpty()) {
            // 判空
            // 如果该类目下没有文章就返回空
            return page;
        }
        List<Integer> articleIds = catalogArticles.stream().map(CatalogArticle::getArticleId).collect(Collectors.toList());
        String sortType = articleDTO.getSortType();
        LambdaQueryWrapper<ArticleInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(ArticleInfo::getId, articleIds);
        if (StringUtils.isEmpty(sortType) || sortType.equals(ArticleDTO.DEFAULT)) {
            // 如果为空或者是默认排序就按照 观看数量进行倒叙排序
            wrapper.orderByDesc(ArticleInfo::getWatchNum);
        } else {
            wrapper.orderByDesc(ArticleInfo::getCreateTime);
        }

        return articleInfoService.page(page, wrapper);
    }

    @Override
    public List<VaccineVO> moreVaccine() {
        List<Catalog> catalogs = this.lambdaQuery().in(Catalog::getType, CatalogEnums.getVaccines()).list();
        if (catalogs.isEmpty()) {
            return new ArrayList<>();
        }
        Map<String, List<Catalog>> collect = catalogs.stream().collect(Collectors.groupingBy(Catalog::getType));
        List<VaccineVO> vaccineVOS = new ArrayList<>();
        VaccineVO vaccineVO;
        for (Map.Entry<String, List<Catalog>> stringListEntry : collect.entrySet()) {
            vaccineVO = new VaccineVO();
            vaccineVO.setTypeName(CatalogEnums.getDescByValue(stringListEntry.getKey()));
            vaccineVO.setTypeImg(CatalogEnums.getImgByValue(stringListEntry.getKey()));
            vaccineVO.setCatalogs(stringListEntry.getValue());
            vaccineVOS.add(vaccineVO);
        }
        return vaccineVOS;
    }


}

